<?php

namespace App\Utils;
use Predis\Client;

class AutogenerationSerialNumber
{
    //自动生成流水号
    public function __construct(Client $redis)
    {
        $this->redis = $redis;
    }
    /**
     * 生成唯一单据号
     * @param String $key 单号KEY
     * @return string
     */
    public function generationSerialNumber($order_id)
    {
        $rule = \App\Models\RuleSerialNumber::first();
        $prefix = $rule->prefix_custom;
        if($rule->date == 1){
            $date = date("Y");
        }
        if($rule->date == 2){
            $date = date("Ym");
        }
        if($rule->date == 3){
            $date = date("Ymd");
        }
        $number = sprintf("%06d",$this->redis->incr($order_id));
        $serialNumber = $prefix.$date.$number;
        return $serialNumber;
    }


}
